#include "editor_win.h"

EditorWin::EditorWin(QWidget *parent)
    : QWidget(parent)
{
    //this->filename = "D:/Object/QtCpp/QtNodeEditor/NodeEditor/nodestyle.qss";
    this->filename = "nodestyle.qss";
    qDebug()<<QDir::currentPath();
    this->LoadStyleSheet(this->filename);

    this->setGeometry(200,200,800,600);
    this->layout = new QVBoxLayout();  // 垂直布局
    this->layout->setContentsMargins(0,0,0,0);  //与四周边缘距离为0
    this->setLayout(this->layout);  // 设置布局


//    this->node = new Node("test");
//    this->nodeScene->AddNode(*node);

    //this->grNode = new NodeGraphicsNode("ttt");
    //this->grScene->addItem(this->grNode);
    //this->nodeScene->graphicsScene->addItem();


    this->editorScene = new EditorScene();  //背景
    this->view = new EditorView(this->editorScene,this);  // 设置view
    this->layout->addWidget(this->view);   //将view添加到布局中



    this->setWindowTitle("Editor");
    this->show();


    //this->AddDebugContent();
}

EditorWin::~EditorWin()
{
    delete layout;
    delete view;
    delete editorScene;
    delete item;
}

void EditorWin::AddDebugContent()
{



    QBrush *brush = new QBrush(Qt::gray);
    QPen *line = new QPen(Qt::black);
    line->setWidth(2);

    QGraphicsRectItem *rect = this->editorScene->addRect(-100,-100,100,100, *line, *brush);
    rect->setFlag(QGraphicsItem::ItemIsMovable);

    QGraphicsTextItem *text = this->editorScene->addText("text");
    text->setFlag(QGraphicsItem::ItemIsSelectable); // 可选择的
    text->setFlag(QGraphicsItem::ItemIsMovable);  // 可移动的
    text->setDefaultTextColor(QColor::fromRgbF(1.0,1.0,1.0));  //改变颜色

//    QPushButton *button = new QPushButton("111");
//    QGraphicsProxyWidget *w1 = this->grScene->addWidget(button);
//    w1->setFlag(QGraphicsItem::ItemIsMovable);
//    w1->setPos(0,50);


//    QTextEdit *edit = new QTextEdit();
//    QGraphicsProxyWidget *w2 = this->grScene->addWidget(edit);
//    w2->setFlag(QGraphicsItem::ItemIsSelectable);
//    w2->setPos(100,200);


}

void EditorWin::addNodeItem(int index)
{
    NodeScene *nodeScene = new NodeScene(index);
    nodeScene->SetIndex(index);
    node.push_back(nodeScene);

    //delete nodeScene

    this->editorScene->addItem(node[node.length()-1]->item);

}

void EditorWin::SetNodeTitle(int index, QString title)
{
    for(int i=0;i<node.length();i++){
        if(node[i]->index == index){
            node[i]->SetItemTitle(title);
        }
    }
}
void EditorWin::DebugNode()
{
//    for(QVector<NodeScene>::iterator *i=node.begin();i!=node.end();i++){
//        qDebug()<<"vector index"<<node[i]->index;
//        qDebug()<<"nodeScene vector index "<<node.at(i);
//    }
    for(int i=0;i<node.length();i++){
        qDebug()<<"vector index"<<node[i]->index<<node[i]->item->title;
    }
}

void EditorWin::AddEdge(int start_node_index, int start_socket_index, int end_node_index, int end_socket_index)
{
    NodeSocket *temp1 = new NodeSocket();
    NodeSocket *temp2 = new NodeSocket();
    NodeScene *node1 = new NodeScene(-1);
    NodeScene *node2 = new NodeScene(-1);
//    NodeItem *item1 = new NodeItem();
//    NodeItem *item2 = new NodeItem();

    for(int i=0;i<node.length();i++){
        // 从output出，连线到input里

        if(node[i]->index == start_node_index){
            for(int j=0;j<node[i]->output.length();j++){
                if(node[i]->output[j]->index == start_socket_index){
                    temp1 = node[i]->output[j];
                    node1 = node[i];
                    //item1 = node[i]->item;
                }
            }
        }
        //TODO: 去重
        if(node[i]->index == end_node_index){
            for(int j=0;j<node[i]->input.length();j++){
                if(node[i]->input[j]->index == end_socket_index){
                    temp2 = node[i]->input[j];
                    node2 = node[i];
                    //item2 = node[i]->item;
                }
            }
        }
    }

    //TODO这里指定父级可能有移动的问题
    //this->edge = new NodeEdge(temp1,temp2,item);
    this->edge = new NodeEdge(node1,node2,temp1,temp2);
    this->edges.append(this->edge);
    //item has already been added to this scene?
    this->editorScene->addItem(this->edge);
}

void EditorWin::RemoveEdge(int start_node_index, int start_socket_index, int end_node_index, int end_socket_index)
{
    //TODO 检测edge是否存在，不存在的处理
    //TODO edges删除
    NodeEdge *remove_edge = new NodeEdge();
    for(int i=0;i<edges.length();i++){
        if(edges[i]->start_node->index == start_node_index &&
                edges[i]->end_node->index == end_node_index &&
                edges[i]->start_socket->index == start_socket_index &&
                edges[i]->end_socket->index == end_socket_index){
            remove_edge = edges[i];
            edges.remove(i);
        }

    }
    this->editorScene->removeItem(remove_edge);
}



void EditorWin::LoadStyleSheet(QString filename)
{
    qDebug()<<"qss:"<<filename;
    QFile file(filename);
    file.open(QFile::ReadOnly | QFile::Text);  // 读取文件全部内容，使用tr()函数将其转换为QString类型
    QString stylesheet = tr(file.readAll());
    //qDebug()<<stylesheet;
    //QApplication::setStyleSheet(stylesheet);
    qApp->setStyleSheet(stylesheet);
    file.close();


}


